//
//  DoctorDetailViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/26.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

enum DoctorReserveStatus: Int {
    case filled
    case unwanted
    
    var canReserve: Bool {
        switch self {
        case .filled: return false
        case .unwanted: return true
        }
    }
    
    var statusText: String {
        switch self {
        case .filled: return "已约满"
        case .unwanted: return "可预约"
        }
    }
    
    var newTextColor: UIColor {
        switch self {
        case .filled: return UIColor(hex: "#999999")
        case .unwanted: return .white
        }
    }
    
    var statusTextColor: UIColor {
        switch self {
        case .filled: return .textLight
        case .unwanted: return .main
        }
    }
    
    var backgroundImage: UIImage? {
        switch self {
        case .filled: return UIImage(color: .background)
        case .unwanted: return UIImage(color: .white)
        }
    }
    
    var textColor: UIColor {
        switch self {
        case .filled: return .textLight
        case .unwanted: return .textDark
        }
    }
    
    var bgColorForGeneral: UIColor {
        switch self {
        case .filled: return .background
        case .unwanted: return .mainColor
        }
    }
}

/// 医生类型 0-图文咨询、云门诊 1-图文咨询 3-云门诊 4-图文和云门诊都没有/门诊挂号
enum DoctorType: Int {
    case all            = 0
    case advisory       = 1
    case cloud          = 3
    case none           = 4
}

enum DoctorSelectType: Int {
    case registration = 0
    case advisory = 1
    case cloud = 2
    case nurse = 3
    
    
    var text: String {
        switch self {
        case .registration: return ""
        case .advisory: return "图文问诊预约"
        case .cloud: return "云门诊预约"
        case .nurse: return "护士预约咨询"
        }
    }
}

struct DoctorDetail: HandyJSON {
    var id: String?
    var deptId: String?
    var docName: String?
    var deptName: String?
    var hospitalName: String?
    var jobTitleName: String?
    var docGoodAtNames: String?
    var docGoodAt: String?
    var bucket: String?
    var num: Int = 0
    var introduction: String?
    var url: String?
    var thumbnailUrl: String?
    var score: Float = 0.0
    var generalScheduleList: [DoctorSchedule]?
    var scheduleInfoDtoList: [DoctorSchedule]?
    var fee: String?
    var diagType: Int = -1
    var isPrescription: Int = 0
    var doctorTeamListDto: [TeamDoctorInfo]?
    
    var tbUserId: String?
}

struct TeamDoctorInfo: HandyJSON {
    ///团队ID
    var id: String?
    ///团队名称
    var name: String?
    ///团队描述
    var description: String?
    ///团队头像
    var url: String?
}

struct TeamDoctorInfoViewModel {
    let model: TeamDoctorInfo
    
    var id: String { return model.id ?? "" }
    var name: String { return model.name ?? "" }
    var description: String { return model.description ?? "" }
    var url: String { return model.url ?? "" }
}

struct DoctorSchedule: HandyJSON {
    var scheduleTime: String?
    var regFee: String?
    var dateTime: String?
    var isSchedule: Int = -1
    var timeInterval: String?
    var scheduledata: String?
    var deptId: String?
    var deptName: String?
}

struct DoctorScheduleViewModel {
    let model: DoctorSchedule
    
    init(model: DoctorSchedule) {
        self.model = model
    }
    
    var scheduleTime: String { return model.scheduleTime ?? "" }
    var regFee: String{ return model.regFee ?? "" }
    var dayTime: String { return model.dateTime ?? "" }
    var status: DoctorReserveStatus { return DoctorReserveStatus(rawValue: model.isSchedule) ?? .unwanted }
    var scheduleDate: String { return model.scheduledata ?? "" }
    var timeInterval: String { return model.timeInterval ?? "" }
    var departmentId: String { return model.deptId ?? "" }
    var departmentName: String { return model.deptName ?? "" }
    
    var cellHeight: CGFloat { return 80 }
}

struct DoctorDetailViewModel {
    let model: DoctorDetail
    
    var generalScheduleList: [DoctorSchedule] { return model.generalScheduleList ?? [] }
    var scheduleList: [DoctorSchedule] { return model.scheduleInfoDtoList ?? [] }
    var doctorTeamListDto: [TeamDoctorInfo] { return model.doctorTeamListDto ?? [] }
    
    var doctorId: String { return model.id ?? "" }
    var departmentId: String { return model.deptId ?? "" }
    var doctorName: String { return model.docName ?? "" }
    var departmentName: String { return model.deptName ?? "" }
    var doctorOffice: String { return model.jobTitleName ?? "" }
    var deptAndOffice: String { return departmentName + " " + doctorOffice }
    var hospitalName: String { return model.hospitalName ?? "" }
    var specialty: String { return model.docGoodAtNames ?? "" }
    var thumbnailUrl: String { return model.thumbnailUrl ?? "" }
    var originalUrl: String { return model.url ?? "" }
    var score: String { return "\(model.score)" }
    var introduction: String { return model.introduction ?? "" }
    var regFee: String { return model.fee ?? "" }
    var doctorType: DoctorType { return DoctorType(rawValue: model.diagType) ?? .all }
    var selectType: DoctorSelectType = .registration
    var isPrescription: Int { return model.isPrescription }
    
    
    var buttonStr: String {
        switch selectType {
        case .registration:
            return ""
        case .advisory:
            if doctorType == .all || doctorType == .advisory {
                return selectType.text + " ￥\(regFee)"
            } else {
                return selectType.text
            }
        case .cloud:
            return selectType.text
        case .nurse:
            return selectType.text
        }
    }
    
    var numberOfRowsForGeneral: Int { return generalScheduleList.count }
    var numberOfRows: Int { return scheduleList.count }
    
    func viewModel(for indexPath: IndexPath) -> DoctorScheduleViewModel {
        return DoctorScheduleViewModel(model: scheduleList[indexPath.row])
    }
    
    func generalViewModel(for indexPath: IndexPath) -> DoctorScheduleViewModel {
        return DoctorScheduleViewModel(model: generalScheduleList[indexPath.row])
    }
    
    func teamViewModel(for indexPath: IndexPath) -> TeamDoctorInfoViewModel {
        return TeamDoctorInfoViewModel(model: doctorTeamListDto[indexPath.item])
    }
    
    
    
    static func getDoctorDetail(doctorId: String,
                                success: @escaping (_ model: DoctorDetail) -> Void,
                                failure: @escaping (_ errorMsg: String?) -> Void)
    {
        let paras = ["id": doctorId]
        HttpTool.shared.request(target: ApiManager.doctorDetail(paras: paras), success: { (json, code) in
            YSGLog("获取医生详情: \(json)")
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<DoctorDetail>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍后重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
}

